package com.api.service.impl;

import com.api.model.dto.userInterfaceinfo.UserInterfaceInfoAddDTO;
import com.api.model.dto.userInterfaceinfo.UserInterfaceInfoQueryDTO;
import com.api.model.dto.userInterfaceinfo.UserInterfaceInfoUpdateDTO;
import com.api.model.entity.InterfaceInfo;
import com.api.model.reslut.ResultPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.api.model.entity.UserInterfaceInfo;
import com.api.service.UserInterfaceInfoService;
import com.api.mapper.UserInterfaceInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author haoshuai
* @description 针对表【user_interface_info(用户调用接口关系)】的数据库操作Service实现
* @createDate 2024-04-26 19:47:53
*/
@Service
public class UserInterfaceInfoServiceImpl extends ServiceImpl<UserInterfaceInfoMapper, UserInterfaceInfo>
    implements UserInterfaceInfoService{



    @Override
    public ResultPage<UserInterfaceInfo> getUserInterfaceInfoPage(UserInterfaceInfoQueryDTO userInterfaceInfoQueryDTO) {
        Integer current = userInterfaceInfoQueryDTO.getCurrent();
        Integer size = userInterfaceInfoQueryDTO.getPageSize();
        IPage<UserInterfaceInfo> page = new Page(current,size);
        baseMapper.selectPage(page,getQueryWrapper(userInterfaceInfoQueryDTO));
        List<UserInterfaceInfo> records = page.getRecords();
        long pages = page.getPages();
        long size1 = page.getSize();
        long total = page.getTotal();
        long current1 = page.getCurrent();
        return new ResultPage<UserInterfaceInfo>(records,total,current1,pages);
    }

    @Override
    public UserInterfaceInfo getUserInterfaceinfoById(Integer id) {
        if (id == null){
            throw new RuntimeException("id不能为空");
        }
        UserInterfaceInfo userInterfaceInfo = baseMapper.selectById(id);
        if (userInterfaceInfo == null){
            throw new RuntimeException("用户不存在");
        }
        return userInterfaceInfo;
    }

    @Override
    public boolean addUserInterfaceInfo(UserInterfaceInfoAddDTO userInterfaceInfoAddDTO) {
        UserInterfaceInfo userInterfaceInfo = new UserInterfaceInfo();
        BeanUtils.copyProperties(userInterfaceInfoAddDTO,userInterfaceInfo);
        return this.save(userInterfaceInfo);
    }

    @Override
    public boolean updateUserInterfaceInfo(UserInterfaceInfoUpdateDTO userInterfaceInfoUpdateDTO) {
        UserInterfaceInfo userInterfaceInfo = new UserInterfaceInfo();
        BeanUtils.copyProperties(userInterfaceInfoUpdateDTO,userInterfaceInfo);
        return this.updateById(userInterfaceInfo);
    }

    @Override
    public boolean deleteUserInterfaceInfo(Integer id) {
        if (id == null){
            throw new RuntimeException("id不能为空");
        }
       return removeById(id);
    }

    private LambdaQueryWrapper<UserInterfaceInfo> getQueryWrapper(UserInterfaceInfoQueryDTO userInterfaceInfoQueryDTO){
        LambdaQueryWrapper<UserInterfaceInfo> queryWrapper = new QueryWrapper<UserInterfaceInfo>().lambda();

        return queryWrapper;
    }
}




